﻿/*	
Copyright (c) 2012, Jeffrey Johnston
Licensed under The MIT License (MIT)
 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Configuration;

namespace InfiniteGeek.Util {
	/// <summary>
	/// Automates the process of tying config values back to strong-typed accessors. 
	/// Also enforces configuration value naming guidelines of "Fully.Qualified.Namespace.IndividualValue"
	/// 
	/// The tricky part is that the base class takes the inheritor as a generic argument to enable reflection.
	/// </summary>
	/// <example><![CDATA[
	/// An implementation of this class looks like this:
	/// 	public class Config : AutoConfig<Config>
	/// 	{
	/// 		static Config() { Init(); } // this is critical
	/// 
	///			[AutoConfigDefault(4096)]
	///			public static Int32 IndividualValue { get; private set; }
	/// 
	///			public static Int32 ValueWithNoDefault { get; private set; }
	///			
	///			public static Int32 UpdateableValueWithNoDefault { get; set; }
	/// 
	///			[AutoConfigIgnore]
	///			public static String ManuallyConfiguredProperty { get; set; }
	///  	}
	/// 
	/// Implementors of this AutoConfig class expect the following in the config file:
	///
	/// 	<configSections>
	/// 		<section name="Fully.Qualified.Namespace" type="System.Configuration.NameValueSectionHandler" />
	/// 	</configSections>
	/// 	<Fully.Qualified.Namespace>
	/// 		<add key="IndividualValue" value="8096" />
	/// 	</Fully.Qualified.Namespace>
	/// 
	/// This will also work, and these values override the section if both are provided:
	/// 
	/// 	<add key="Fully.Qualified.Namespace.IndividualValue" value="65535" />
	/// 
	/// ]]></example>
	public abstract class AutoConfig<TConfig> where TConfig : new() {
		/// <summary>Uses reflection to find auto-properties on inheriting class and populate with matching config values.</summary>
		/// <remarks>Good golly I love reflection.</remarks>
		/// <exception cref="ConfigurationErrorsException"><c>ConfigurationErrorsException</c>.</exception>
		protected internal static void Init() {
			AutoConfigParser<TConfig>.Parse();
		}
	}
}